package com.key.controller.admin.systemSetting;

import com.github.pagehelper.PageHelper;
import com.key.common.R;
import com.key.entity.SysMenu;
import com.key.entity.SysRole;
import com.key.service.SysRoleService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @Author：朕
 * @Package：com.key.controller.admin.systemSetting
 * @Project：ams_system
 * @name：SysRoleController
 * @Date：2023/2/18 9:34
 * @Filename：SysRoleController
 */
@Controller
@Api(value = "系统角色",tags = "系统角色API")
@RequestMapping(value = "/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;



    @RequestMapping(value = "/findRole",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "显示系统角色",notes = "",httpMethod = "get", response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前页",paramType = "query",dataType = "int",required = true),
            @ApiImplicitParam(name = "pageSize",value = "页面大小",paramType = "query",dataType = "int",required = true)
    })
    public R findRole(Integer currentPage,Integer pageSize){
        R r = new R();
        PageHelper.startPage(currentPage,pageSize);
        List<SysRole> sysRoles = sysRoleService.findRole();
        if(sysRoles.size()>0){
            r.setCode(200);
            r.setMsg("显示成功");
            r.setData(sysRoles);
            return r;
        }else{
            r.setCode(500);
            r.setMsg("显示失败");
            return r;
        }
    }

    @RequestMapping(value = "/addSysRole",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "系统角色的增加",notes = "传递角色实体类信息",httpMethod = "post", response = R.class)
    public R addSysRole(@RequestBody @ApiParam(name = "SysRole",value = "系统角色的实体类",required = true)SysRole sysRole){
        R r = new R();
        boolean s = sysRoleService.addRole(sysRole);
        if(s==true){
            r.setCode(200);
            r.setMsg("增加成功");
            return r;
        }else{
            r.setCode(500);
            r.setMsg("增加失败");
            return r;
        }
    }

    @RequestMapping(value = "/deleteRole",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "系统角色的删除",notes = "角色的ID",httpMethod = "post", response = R.class)
    public R deleteRole(@RequestBody @ApiParam(name = "RoleId",value = "系统角色的Id",required = true)Integer roleId){
        R r = new R();
        boolean s = sysRoleService.deleteRole(roleId);
        if(s==true){
            r.setCode(200);
            r.setMsg("删除成功");
            return r;
        }else{
            r.setCode(500);
            r.setMsg("删除失败");
            return r;
        }
    }



    @RequestMapping(value = "/findSysRoleByName",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据角色名称查询系统角色",notes = "传递查询的角色名称",httpMethod = "get", response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "currentPage",value = "当前页",paramType = "query",dataType = "int",required = true),
            @ApiImplicitParam(name = "pageSize",value = "页面大小",paramType = "query",dataType = "int",required = true),
            @ApiImplicitParam(name = "roleTitle",value = "角色名称",paramType = "query",dataType = "String",required = true)
    })
    public R findSysRoleByName(Integer currentPage,Integer pageSize,String roleTitle){
        R r = new R();
        PageHelper.startPage(currentPage,pageSize);
        List<SysRole> s = sysRoleService.findRoleByRoleTitle(roleTitle);
        if(s != null){
            r.setCode(200);
            r.setMsg("查询成功");
            r.setData(s);
            return r;
        }else{
            r.setCode(500);
            r.setMsg("查询失败");
            return r;
        }
    }



    @RequestMapping(value = "/updateRole",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "系统角色的修改",notes = "角色实体接受修改内容",httpMethod = "post", response = R.class)
    public R updateRole(@RequestBody @ApiParam(name = "SysRole",value = "系统角色的实体类",required = true)SysRole sysRole){
        R r = new R();
        boolean s = sysRoleService.updateRole(sysRole);
        if(s==true){
            r.setCode(200);
            r.setMsg("修改成功");
            return r;
        }else{
            r.setCode(500);
            r.setMsg("修改失败");
            return r;
        }
    }


    @RequestMapping(value = "/findMenu",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "系统权限的显示",httpMethod = "get", response = R.class)
    public R findMenu(){
        R r = new R();
        List<SysMenu> sysMenus = sysRoleService.findMenu();
        if(sysMenus != null){
            r.setCode(200);
            r.setMsg("权限显示成功");
            r.setData(sysMenus);
            return r;
        }else{
            r.setCode(500);
            r.setMsg("权限显示失败");
            return r;
        }
    }

    @RequestMapping(value = "/findMenuByRoleId",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "根据角色Id查找权限",notes = "角色ID",httpMethod = "get",response = R.class)
    public R findMenuByRoleId(@RequestBody @ApiParam(name = "roleId",value = "角色Id",required = true)Integer roleId){
        R r = new R();
        List<SysMenu> sysMenus = sysRoleService.findMenuByRoleId(roleId);
        if(sysMenus != null){
            r.setCode(200);
            r.setMsg("权限查找成功");
            r.setData(sysMenus);
            return r;
        }else{
            r.setCode(500);
            r.setMsg("权限查找失败");
            return r;
        }
    }


    @RequestMapping(value = "/addUserRole",method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "增加用户角色",notes = "传递用户ID和角色ID",httpMethod = "post",response = R.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId",value = "角色Id",required = true,dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "menuId",value = "权限Id",required = true,dataType = "String",paramType = "query")
    })
    public R addRoleMenu(Integer roleId,String menuId){
        R r = new R();
        boolean s = sysRoleService.deleteAddRoleMenu(roleId,menuId);
        if(s==true){
            r.setCode(200);
            r.setMsg("角色获取权限成功");
            return r;
        }else{
            r.setCode(500);
            r.setMsg("角色获取权限失败");
            return r;
        }
    }


}
